package com.module.wx.controller;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.module.wx.domain.ZzdOrderListInfo;
import com.module.wx.service.IZzdOrderListInfoService;
import com.module.wx.util.Util;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.module.wx.domain.ZzdOrderList;
import com.module.wx.service.IZzdOrderListService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author hezhengxian
 * @date 2023-02-10
 */
@RestController
@RequestMapping("/orderlist")
public class ZzdOrderListController extends BaseController
{
    @Autowired
    private IZzdOrderListService zzdOrderListService;
    @Autowired
    private IZzdOrderListInfoService zzdOrderListInfoService;

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("wx:orderlist:list")
    @GetMapping("/list")
    public TableDataInfo list(ZzdOrderList zzdOrderList)
    {
        startPage();
        List<ZzdOrderList> list = zzdOrderListService.selectZzdOrderListList(zzdOrderList);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("wx:orderlist:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZzdOrderList zzdOrderList)
    {
        List<ZzdOrderList> list = zzdOrderListService.selectZzdOrderListList(zzdOrderList);
        ExcelUtil<ZzdOrderList> util = new ExcelUtil<ZzdOrderList>(ZzdOrderList.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @RequiresPermissions("wx:orderlist:query")
    @GetMapping(value = "/{orderNo}")
    public AjaxResult getInfo(@PathVariable("orderNo") String orderNo)
    {
        return success(zzdOrderListService.selectZzdOrderListByOrderNo(orderNo));
    }

    /**
     * 新增【请填写功能名称】
     */
    @RequiresPermissions("wx:orderlist:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ZzdOrderList zzdOrderList)
    {
        // 添加随机id
        zzdOrderList.setOrderNo(Util.get_radom_id());
        zzdOrderList.setOrderTime(new Date());
        return toAjax(zzdOrderListService.insertZzdOrderList(zzdOrderList));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("wx:orderlist:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping("/updateOrderList")
    public AjaxResult edit(@RequestBody ZzdOrderList zzdOrderList)
    {
        return toAjax(zzdOrderListService.updateZzdOrderList(zzdOrderList));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("wx:orderlist:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderNos}")
    public AjaxResult remove(@PathVariable String[] orderNos)
    {
        return toAjax(zzdOrderListService.deleteZzdOrderListByOrderNos(orderNos));
    }

    /**
     * 用户下单，新增主订单、子订单信息
     */
    @RequiresPermissions("wx:orderlist:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PutMapping("/addOrderList")
    public AjaxResult addOrderList(@RequestBody ZzdOrderList zzdOrderList)
    {
        // 添加随机id
        zzdOrderList.setOrderNo(Util.get_radom_id());
        zzdOrderList.setOrderTime(new Date());
        zzdOrderList.setIsTrue("0");
        // 添加主订单信息
        //zzdOrderListService.insertZzdOrderList(zzdOrderList);
        // 是否购买了多个菜品
        if(zzdOrderList.getInfoList().size()>0){
            for (int i = 0; i < zzdOrderList.getInfoList().size(); i++) {
                ZzdOrderListInfo orderListInfo = zzdOrderList.getInfoList().get(i);
                orderListInfo.setId(Util.get_radom_id());
                // 将主订单id添加到子订单信息中
                orderListInfo.setOrderNo(zzdOrderList.getOrderNo());
                // 插入订单子表数据
                zzdOrderListInfoService.insertZzdOrderListInfo(orderListInfo);
            }
        }
        return toAjax(zzdOrderListService.insertZzdOrderList(zzdOrderList));
    }

    /**
     * 根据用户id查询订单信息
     */
    @RequiresPermissions("wx:orderlist:list")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/selectOrderList/{userId}")
    public AjaxResult selectOrderInfoByUserId(@PathVariable("userId")String userId) throws SQLException, ClassNotFoundException {
        AjaxResult ajax = AjaxResult.success();
        if(userId !=null&&!userId.equals("")){
           List<ZzdOrderList> zzdOrderLists = zzdOrderListService.selectOrderInfoByUserId(userId);
           if(zzdOrderLists.size()>0){
               for (int i = 0; i < zzdOrderLists.size(); i++) {
                   List<ZzdOrderListInfo> list = Util.getOrderListDataByOrderNo("select  * from zzd_order_list_info where order_no="+zzdOrderLists.get(i).getOrderNo());
                   zzdOrderLists.get(i).setInfoList(list);
               }
           }
            ajax.put("zzdOrderList",zzdOrderLists);

        }
        return ajax;
    }

    /**
     *  根据主订单id批量删除子订单信息
     * @param param
     * @return
     */
    @RequiresPermissions("wx:orderlist:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeOrderList")
    public AjaxResult removeOrderList(@RequestBody Map<String,String[]> param)
    {
        String[] orderNos = null;
        AjaxResult ajax = new AjaxResult();
        if(param!=null){
            // 传入的数组参数
            orderNos = param.get("orderNos");
            // 需删除的订单编号不为空且长度不为0
            if(orderNos!=null&&orderNos.length>0){
                for (int i = 0; i < orderNos.length; i++) {
                    // 更新删除标志
                    ZzdOrderList zzdOrderList = zzdOrderListService.selectZzdOrderListByOrderNo(orderNos[i]);
                    // 已删除
                    zzdOrderList.setIsTrue("8");
                    zzdOrderListService.updateZzdOrderList(zzdOrderList);
                }
                // 根据主订单批量删除子订单信息
                //zzdOrderListInfoService.deleteZzdOrderListInfoByParentId(orderNos);
                ajax.put("msg","操作成功");
                ajax.put("code",200);
            }
        }
        // return toAjax(zzdOrderListService.deleteZzdOrderListByOrderNos(orderNos));
        return ajax;
    }

    /**
     *  订单付款处理
     * @param zzdOrderList
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @RequiresPermissions("wx:orderlist:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/orderPay")
    public AjaxResult orderPay(@RequestBody ZzdOrderList zzdOrderList) {
        AjaxResult ajax = AjaxResult.success();
        if(zzdOrderList !=null){
            // 获取订单相关金额信息，进行微信支付，获取是否付款成功状态

            //付款成功后，查询餐柜空余数量，且指定餐柜编号何取餐码
        }
        return ajax;
    }
}
